import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class DomTrackService {

  private _created = 0;
  private _reused = 0;
  private _destroyed = 0;


  private _statsSubject: BehaviorSubject<{ created: number; reused: number; destroyed: number }> =
    new BehaviorSubject({ created: 0, reused: 0, destroyed: 0 });


  stats$: Observable<{ created: number; reused: number; destroyed: number }> = this._statsSubject.asObservable();

  constructor() { }


  reset(): void {
    this._created = 0;
    this._reused = 0;
    this._destroyed = 0;
    this._statsSubject.next({ created: this._created, reused: this._reused, destroyed: this._destroyed });
  }


  incrementCreated(): void {
    this._created++;
    this._statsSubject.next({ created: this._created, reused: this._reused, destroyed: this._destroyed });
  }

  incrementReused(): void {
    this._reused++;
    this._statsSubject.next({ created: this._created, reused: this._reused, destroyed: this._destroyed });
  }


  incrementDestroyed(): void {
    this._destroyed++;
    this._statsSubject.next({ created: this._created, reused: this._reused, destroyed: this._destroyed });
  }


  updateStats(created: number, reused: number, destroyed: number): void {
    this._created = created;
    this._reused = reused;
    this._destroyed = destroyed;
    this._statsSubject.next({ created: this._created, reused: this._reused, destroyed: this._destroyed });
  }


  get created(): number {
    return this._created;
  }


  get reused(): number {
    return this._reused;
  }


  get destroyed(): number {
    return this._destroyed;
  }
}
